package com.project.service.base;


import com.project.entity.User;
import com.project.myBeans.ConfigParameters;
import com.project.myBeans.ResponseBean;
import com.project.shiro.RetryLimitHashedCredentialsMatcher;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 基础服务
 */
public class BaseService {
    protected Logger log = LogManager.getLogger(getClass());

    public ModelAndView view = new ModelAndView();

    @Autowired
    public ConfigParameters configParameters;

    @Autowired
    public ResponseBean responseBean;

    //用户登录处理(待解决)
    public boolean userLogin(Subject subject, HttpServletRequest request, String username, String password) //session
    {
        if ("get".equalsIgnoreCase(request.getMethod())) {
            return false;
        }

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return false;
        }

        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            subject.login(token);
            return true;
        } catch (ExcessiveAttemptsException e) {
//            message.setCode(AppConstant.USERNAME_NOTEXIST);
//            message.setMsg("帐号被锁定10分钟");
            Cache<String, AtomicInteger> cache = RetryLimitHashedCredentialsMatcher.getPasswordRetryCache();
            if (null == cache.get(username + "locked")) {
                cache.put(username + "locked", new AtomicInteger(0));
            }
            e.printStackTrace();
            return false;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {

        }
    }


    //过滤敏感信息
    public void filterSensitiveInfo(User user) {
//        user.setUsername(null);
        user.setPassword(null);
        user.setSalt(null);
    }
    /**
     * author: Tatebrown
     * date: 2018/5/25
     * function: 检查删除/更新是否成功
     *
     * @param effectCol 影响行数
     * @return
     */
    public boolean checkDeleteUpdateIsSuccess(int effectCol) {
        boolean flag = false;
        if (effectCol > 0) flag = true;
        return flag;
    }
    //生成六位验证码
    public String getSmsCode() {
        Random random = new Random();
        int codeLength = 6;
        String code = "";
        int n;
        while (true) {
            n = random.nextInt(10);
//                if(code.contains(String.valueOf(n))) //控制验证码不出现重复数字
//                    continue;
            code += n;
            if (code.length() == codeLength) //验证码6位
                break;
        }
        return code;
    }
     //获取邮箱服务器其相关参数
    public JavaMailSenderImpl getMailSender(){
        String emailHost="";
        String emailUserName="";
        String emailPassword="";
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        try {
//            Properties pro = new Properties();//属性集合对象
//            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
//            pro.load(inputStream);//将属性文件流装载到Properties对象中
            emailHost = configParameters.getEmailHost();
            emailUserName = configParameters.getEmailUserName();
            emailPassword = configParameters.getEmailPassword();

            //定义邮件发送器

            mailSender.setHost(emailHost);
            mailSender.setUsername(emailUserName);
            mailSender.setPassword(emailPassword);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return  mailSender;
    }
    //WheelChen: 检查删除/更新是否成功
    /**
     * author: WheelChen
     * date: 2017/7/24
     * function: 检查删除/更新是否成功
     *
     * @param effectCol 受影响行数
     * @return
     */
    public boolean processSuccessDeleteUpdate(int effectCol) {
        if (effectCol > 0) {
           return true;
        } else {
          return false;
        }
    }
}
